package com.northwest_man.refactor_tool;

import java.util.HashMap;
import java.util.Map;

public class MapComparator {
    public static void main(String[] args) {
        // Xml
        final Map<String, Map<String, String>> A = ReaderForXml.read();
        // Excel
        final Map<String, Map<String, String>> B = ReaderForExcel.read();
        compare(A, B);
    }

    private static final String[] excludes = new String[]{
            "tel_with_colon", "order_date_with_colon", "android 没用该文案",
            "login_and_security",
            "only_left_in_stock",
            "name_with_colon",
            "address_with_colon"
    };
    private static boolean equals(final String a, final String b) {
        final boolean emptyA = empty(a);
        final boolean emptyB = empty(b);
        if (emptyA && emptyB) return true;
        if (emptyA) return false;
        if (emptyB) return false;
        return a.contains(b) || b.contains(a);
    }

    private static boolean empty(final String text) {
        return text == null || text.trim().isEmpty();
    }

    private static boolean exclude(final String key) {
        for (String exclude : excludes) {
            if (exclude.equals(key)) return true;
        }
        return false;
    }
    public static void compare(final Map<String, Map<String, String>> A, final Map<String, Map<String, String>> B) {


        // 假设A和B已经填充了相应的数据
        // ...

        Map<String, Map<String, String>> A_B = new HashMap<>();
        Map<String, Map<String, String>> B_A = new HashMap<>();

        // 遍历A中的每个语言
        for (String language : A.keySet()) {
            // 检查B中是否存在相同的语言
            if (B.containsKey(language)) {
                Map<String, String> languageDataA = A.get(language);
                Map<String, String> languageDataB = B.get(language);

                Map<String, String> diffDataA_B = new HashMap<>();
                Map<String, String> diffDataB_A = new HashMap<>();

                // 遍历A中当前语言的键值对
                for (String key : languageDataA.keySet()) {
                    if (exclude(key)) continue;
                    String valueA = languageDataA.get(key);
                    if (valueA != null && valueA.contains("%s")) continue;
                    // 检查B中当前语言是否包含相同的键
                    if (languageDataB.containsKey(key)) {
                        String valueB = languageDataB.get(key);

                        if (valueB != null && valueB.contains("%s")) continue;
                        // 比较A和B中相同键对应的值是否相等
                        if (!equals(valueA, valueB)) {
                            // 将不同的键值对添加到diffDataA_B和diffDataB_A中
                            diffDataA_B.put(key, String.format("valueA: [%s] | valueB: [%s]", valueA, valueB));

                            diffDataB_A.put(key, String.format("valueA: [%s] | valueB: [%s]", valueA, valueB));
                        }
                    } else {

                        if (valueA != null && !valueA.trim().isEmpty()) {
                            // B中不包含当前键，将当前键值对添加到diffDataA_B中
                            diffDataA_B.put(key, String.format("valueA: [%s] | valueB: [%s]", valueA, "无"));


                        }
                    }
                }

                // 遍历B中当前语言的键值对，检查是否存在A中没有的键
                for (String key : languageDataB.keySet()) {
                    if (exclude(key)) continue;
                    if (!languageDataA.containsKey(key) && !empty(languageDataB.get(key))) {
                        String valueB = languageDataB.get(key);
                        // 将B中不同的键值对添加到diffDataB_A中


                        diffDataB_A.put(key, String.format("valueA: [%s] | valueB: [%s]", "无", valueB));

                    }
                }

                // 将diffDataA_B添加到A_B中
                A_B.put(language, diffDataA_B);
                // 将diffDataB_A添加到B_A中
                B_A.put(language, diffDataB_A);
            } else {
                // B中不包含当前语言，将A中当前语言的所有键值对添加到A_B中
                A_B.put(language, A.get(language));
            }
        }

        // 遍历B中的每个语言，检查是否存在A中没有的语言
        for (String language : B.keySet()) {
            if (!A.containsKey(language)) {
                // 将B中当前语言的所有键值对添加到B_A中
                B_A.put(language, B.get(language));
            }
        }

        // 输出A_B和B_A的结果
        System.out.println("Xml 有 - Excel 无:");
        printMapOfMaps(A_B);
//        System.out.println("\nExcel 有 - Xml 无:");
//        printMapOfMaps(B_A);
    }

    // 辅助方法用于输出Map<String, Map<String, String>>类型的变量
    private static void printMapOfMaps(Map<String, Map<String, String>> map) {
        for (String language : map.keySet()) {
            System.out.println("Language: " + language);
            Map<String, String> languageData = map.get(language);
            for (String key : languageData.keySet()) {
                String value = languageData.get(key);
                System.out.println("Key: " + key + ", Value: " + value);
            }
        }
    }
}